// pages/myOrder/myOrder.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import calcUtils from '../../utils/calcUtil.js';
import myUtils from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    tabs: [
      { id: "nav_1", isSelect: false, title: "全部", value: 0, isFirst: true },
      { id: "nav_2", isSelect: false, title: "待付款", value: 1, isFirst: true },
      { id: "nav_3", isSelect: false, title: "待发货", value: 2, isFirst: true },
      { id: "nav_4", isSelect: false, title: "待收货", value: 3, isFirst: true },
    ],

    allOrderList: [], //全部订单列表
    allOrderCurrent: 1,
    allOrderHasNextPage: true,

    payOrderList: [], //待付款列表
    payOrderCurrent: 1,
    payOrderHasNextPage: true,

    sendOrderList: [],//待发货
    sendOrderCurrent: 1,
    sendOrderHasNextPage: true,

    takeOrderList: [],//待收货
    takeOrderCurrent: 1,
    takeOrderHasNextPage: true,
  },
  /**
   * 申请退款
   */
  bankMoney:function(){
    wx.showModal({
      title: '提示',
      content: '请下载服装基地APP后申请退款',
      showCancel: false
    })
  },
  /**
   * 获取我的订单
   */
  findMyOrders: function (activeIndexType) {
    var that = this;

    var activeIndex = that.data.activeIndex;
    if (!myUtils.isBlank(activeIndexType)){
      activeIndex = activeIndexType;
    }
    var current = 1;
    var status = 1;
    if (activeIndex == 0) {
      current = that.data.allOrderCurrent;
      status = '';
    }
    if (activeIndex == 1) {
      current = that.data.payOrderCurrent;
      status = 1;
    }
    if (activeIndex == 2) {
      current = that.data.sendOrderCurrent;
      status = 2;
    }
    if (activeIndex == 3) {
      current = that.data.takeOrderCurrent;
      status = 3;
    }

    httpsUtils.getData({
      _url: app.globalData._url + '/order/selectOrderForGoodsList1?current=' + current + "&status=" + status,
      data: '',
    }, function (json) {
      if (json.success == false && json.code == 'OVERTIME') {
        wx.showModal({
          title: '登陆提醒',
          content: '需要您登陆才能查看',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../wxLogin/wxLogin?type=9&isTab=false',
              })
            } else{
              wx.switchTab({
                url: '../myinfo/myinfo',
              })
            }
          }
        })
      } else if (json.data && json.success) {
        //计算商品分类价格
        var orderList = json.data.list;
        for (let shop of orderList) {
          for (let goodCar of shop.goodsCarList) {
            var goodsTypeMoney = 0.00;
            goodsTypeMoney = calcUtils.mul(goodCar.goodsNumber, goodCar.packRate);
            goodCar.goodCarMoney = goodsTypeMoney.toFixed(2);

            for (let myStock of goodCar.myStockList) {
              var stockMoney = 0.00;
              stockMoney = calcUtils.mul(myStock.saleNum, goodCar.packRate);
              myStock.goodCarMoney = stockMoney.toFixed(2);
            }
          }
          shop.freightMoney = shop.freightMoney.toFixed(2);
          shop.payMoney = shop.payMoney.toFixed(2);
        }
       
        if (activeIndex == 0) {
          that.setData({
            allOrderList: that.data.allOrderList.concat(json.data.list),
            allOrderCurrent: json.data.pageNum,
            allOrderHasNextPage: json.data.hasNextPage,
          })
        }
        if (activeIndex == 1) {
          that.setData({
            payOrderList: that.data.payOrderList.concat(json.data.list),
            payOrderCurrent: json.data.pageNum,
            payOrderHasNextPage: json.data.hasNextPage,
          })
        }
        if (activeIndex == 2) {
          that.setData({
            sendOrderList: that.data.sendOrderList.concat(json.data.list),
            sendOrderCurrent: json.data.pageNum,
            sendOrderHasNextPage: json.data.hasNextPage,
          })
        }
        if (activeIndex == 3) {
          that.setData({
            takeOrderList: that.data.takeOrderList.concat(json.data.list),
            takeOrderCurrent: json.data.pageNum,
            takeOrderHasNextPage: json.data.hasNextPage,
          })
        }
      }
    })
  },
  /**
   * 初始化选中
   */
  initSelect: function (types) {
    var that = this;
    var activeIndex = parseInt(types);
    var tabs = that.data.tabs;
    for (let obj of tabs) {
      if (obj.value == activeIndex) {
        obj.isSelect = true;
        obj.isFirst = false;
      }
    }
    that.setData({
      activeIndex: activeIndex,
      tabs: tabs,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //初始化选中
    that.initSelect(options.type);
    //初始化订单
    that.findMyOrders();
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    var activeIndex = that.data.activeIndex;

    if (activeIndex == 0) {
      if (that.data.allOrderHasNextPage) {
        that.setData({
          allOrderCurrent: that.data.allOrderCurrent + 1,
        })
        that.findMyOrders();
      }
    }
    if (activeIndex == 1) {
      if (that.data.payOrderHasNextPage) {
        that.setData({
          payOrderCurrent: that.data.payOrderCurrent + 1,
        })
        that.findMyOrders();
      }
    }
    if (activeIndex == 2) {
      if (that.data.sendOrderHasNextPage) {
        that.setData({
          sendOrderCurrent: that.data.sendOrderCurrent + 1,
        })
        that.findMyOrders();
      }
    }
    if (activeIndex == 3) {
      if (that.data.takeOrderHasNextPage) {
        that.setData({
          takeOrderCurrent: that.data.takeOrderCurrent + 1,
        })
        that.findMyOrders();
      }
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 头部导航
   */
  tabClick: function (e) {
    var that = this;
    var tabs = that.data.tabs;
    var index = e.currentTarget.id;
    var isFirst = false;
    for (let obj of tabs) {
      if (obj.value == index) {
        isFirst = obj.isFirst;
        obj.isFirst = false;
      }
    }
    that.setData({
      tabs: tabs,
      activeIndex: e.currentTarget.id
    });

    //更新数据，第一次点击时候加载重新加载数据
    if (isFirst) {
      that.findMyOrders();
    }
  },
  /**
   *提醒发货
   */
  remindOrder: function (e) {
    var that = this;
    var orderNumber = e.currentTarget.dataset.orderNumber;
    httpsUtils.getData({
      _url: app.globalData._url + '/user/remindSendGoods1?orderNumber=' + orderNumber,
      data: {},
    }, function (json) {
      if (json.success) {
        wx.showToast({
          title: '提醒发货成功',
        })
      } else {
        wx.showToast({
          title: json.msg,
        })
      }
    })
  },
  /**
   *确认收货
   */
  confirmTakeOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderId;
    httpsUtils.getData({
      _url: app.globalData._url + '/order/confirmOrderByOrderId?orderId=' + orderId,
      data: {},
    }, function (json) {
      if (json.success) {
        wx.showToast({
          title: '确认收货成功',
        })
        that.setData({
          allOrderList: [],
          takeOrderList: [],
        })
        that.findMyOrders(3);
        that.findMyOrders(0);
        
      } else {
        wx.showToast({
          title: json.msg,
        })
      }
    })
  },
  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    var that = this;
    var orderNumber = e.currentTarget.dataset.orderNumber;
    var orderType = e.currentTarget.dataset.type;
    wx.showModal({
      title: '提示',
      content: '你确定要取消订单吗？',
      success: function (res) {
        if (res.confirm) {
          httpsUtils.getData({
            _url: app.globalData._url + '/order/cancelOrder?orderNumber=' + orderNumber + "&type=" + orderType,
            data: {},
          }, function (json) {
            if (json.success) {
              wx.showToast({
                title: '取消成功',
              })
              that.setData({
                allOrderList: [],
                payOrderList: [],
              })
              that.findMyOrders(1);
              that.findMyOrders(0);
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 评论订单
   */
  commentOrder: function () {
    wx.showModal({
      title: '提示',
      content: '请前往服装基地APP评论',
      showCancel: false
    })
  },
  /**
   * 已完成订单
   */
  okOrder: function () {
    wx.showToast({
      title: '订单已完成',
      icon:'none',
    })
  },
  /**
   *删除订单
   */
  delOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderId;
    wx.showModal({
      title: '提示',
      content: '你确定要删除订单吗？',
      success: function (res) {
        if (res.confirm) {
          httpsUtils.postData({
            _url: app.globalData._url + '/order/update1',
            data: { id: orderId, status: 4 },
          }, function (json) {
            if (json.success) {
              wx.showToast({
                title: '删除成功',
              })
              that.setData({
                allOrderList: [],
              })
              that.findMyOrders(0);
            } else {
              wx.showToast({
                title: json.msg,
              })
            }
          })
        } else if (res.cancel) {
        }
      }
    })
   
  },
  /**
   * 去支付
   */
  confirmOrder: function (e) {
    var payOrders = [];
    payOrders.push({ orderId: e.currentTarget.dataset.orderId, orderMoney: e.currentTarget.dataset.orderMoney, payMoney: e.currentTarget.dataset.payMoney, freightMoney: e.currentTarget.dataset.freightMoney, goodsNumber: e.currentTarget.dataset.goodsNumber })
    wx.setStorageSync('payOrders', payOrders);
    wx.navigateTo({
      url: '../pay/pay',
    })
  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})